#------------------------------------------------------------------------------
# generate unique geo location data
#==============================================================================

load_library = c('bit64','data.table','fst','future.apply','stringr','logger','vroom')
invisible(lapply(load_library, function(x) library(x, character.only=TRUE, quietly= TRUE)))

# read arguments
args=commandArgs(TRUE)
infile_mbr = args[[1]]
outfile = args[[2]]

bucket = '/N/project/iuni_doctorshopping'

# read data
mbr = read_fst(infile_mbr, columns=c('PATID','DIVISION','LIS_DUAL','PRODUCT','ELIGEFF','ELIGEND'), as.data.table = TRUE)

# measure the length of stays
mbr[,length_stay := as.numeric(ELIGEND - ELIGEFF,units='days')]
mbr = mbr[, .(length_stay = sum(length_stay)), by=c('PATID','DIVISION','LIS_DUAL','PRODUCT')]

# select the division that has the longest residential duration
unique_mbr = mbr[mbr[,.I[which.max(length_stay)],by="PATID"]$V1]

# export data 
fwrite(unique_mbr, outfile)
